package com.binhthuan.catechist.client.data;

import com.binhthuan.catechist.shared.constant.Parameter;
import com.binhthuan.catechist.shared.constant.Service;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.FieldValueExtractor;
import com.smartgwt.client.data.XMLTools;
import com.smartgwt.client.data.fields.DataSourceBooleanField;
import com.smartgwt.client.data.fields.DataSourceDateField;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.widgets.form.fields.SelectItem;

public class PersonDS extends AbstractDS {

    private static PersonDS instance;

    private PersonDS() {
        this(Service.PERSON);
    }

    /**
     * @param id
     */
    public PersonDS(String id) {
        super(id);

        DataSourceIntegerField idField = new DataSourceIntegerField(Parameter.ID);
        idField.setHidden(true);
        idField.setPrimaryKey(true);

        DataSourceTextField addressField = new DataSourceTextField(Parameter.ADDRESS);

        SelectItem areaEditor = new SelectItem(Parameter.AREA);
        areaEditor.setOptionDataSource(AreaDS.getInstance());
        areaEditor.setValueField(Parameter.ID);
        areaEditor.setDisplayField(Parameter.NAME);
        areaEditor.setDefaultToFirstOption(true);

        DataSourceTextField areaField = new DataSourceTextField(Parameter.AREA);
        areaField.setValueXPath(combineXPath(Parameter.AREA, Parameter.ID));
        areaField.setTypeAsDataSource(AreaDS.instance);
        areaField.setEditorType(areaEditor);

        DataSourceTextField avatarField = new DataSourceTextField(Parameter.AVATAR);

        DataSourceDateField birthdayField = new DataSourceDateField(Parameter.BIRTHDAY);

        DataSourceTextField firstNameField = new DataSourceTextField(Parameter.FIRST_NAME);
        firstNameField.setRequired(true);

        DataSourceTextField lastNameField = new DataSourceTextField(Parameter.LAST_NAME);
        lastNameField.setRequired(true);

        DataSourceBooleanField genderField = new DataSourceBooleanField(Parameter.MALE);

        DataSourceTextField patronSaintField = new DataSourceTextField(Parameter.PATRON_SAINT);

        DataSourceTextField phoneField = new DataSourceTextField(Parameter.PHONE);

        DataSourceTextField fullName = new DataSourceTextField(Parameter.FULL_NAME);
        
        fullName.setFieldValueExtractor(new FieldValueExtractor() {
            
            @Override
            public Object execute(Object record, Object value, DataSourceField field, String fieldName) {
                String lastName = XMLTools.selectObjects(record, Parameter.LAST_NAME).getJavaScriptObject().toString();
                String firstName = XMLTools.selectObjects(record, Parameter.FIRST_NAME).getJavaScriptObject().toString();
                return (lastName + " " + firstName).trim();
            }
        });
        DataSourceBooleanField activeField = new DataSourceBooleanField(Parameter.ACTIVE);
        DataSourceBooleanField temporaryDeleteField = new DataSourceBooleanField(Parameter.TEMPORARY_DELETE);

        setFields(idField, addressField, areaField, avatarField, birthdayField, firstNameField, lastNameField, genderField, patronSaintField, phoneField, fullName, activeField, temporaryDeleteField);
    }

    public int getType() {
        return 0;
    }

    public static PersonDS getInstance() {
        if (instance == null) {
            instance = new PersonDS();
        }

        return instance;
    }
}
